package test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;


class User implements Comparable<User> {
	private int age;

	public User(int age) {
		this.age = age;
	}

	public int getAge() {
		return age;
	}

	@Override
	public int compareTo(User o) {

		return age - o.age;
	}

	@Override
	public String toString() {
		return "User [age=" + age + "]";
	}

}

class StuUser extends User {
	private String no;

	public StuUser(int age, String no) {
		super(age);
		this.no = no;
	}

	public String getNo() {
		return no;
	}

	@Override
	public String toString() {
		return "StuUser [no=" + no + ", toString()=" + super.toString() + "]";
	}

}

class UserReverseComparator implements Comparator<User> {

	@Override
	public int compare(User o1, User o2) {
		return o2.getAge() - o1.getAge();
	}

}

class StuUserComparator implements Comparator<StuUser> {

	@Override
	public int compare(StuUser o1, StuUser o2) {
		return o1.getNo().compareTo(o2.getNo());
	}

}

public class GenericMain {

	public static <T extends Comparable<T>> T max(List<T> list) {
		T Max=list.get(0);
		for(T e:list){
			if(e.compareTo(Max)>0)
				Max=e;
		}
		return Max;
	}
	public static <StuUserComparator extends Comparable<StuUser>> StuUser max1(List<StuUser> list) {
		StuUser Max=list.get(0);
		for(StuUser e:list){
			if(e.compareTo(Max)>0)
				Max=e;
		}
		return Max;
	}
	public static <T> int myCompare(T o1, T o2, Comparator<T> c) {
		return c.compare(o1, o2);
	}
	public static void main(String[] args) {
		List<String>strList=new  ArrayList<String>();
		List<Integer>intList=new  ArrayList<Integer>();
		strList.add("Ad");
		strList.add("Cv");
		strList.add("EE");
		strList.add("aww");
		intList.add(42);
		intList.add(231);
		intList.add(1);
		intList.add(112);
		System.out.println(max(strList));
		System.out.println(max(intList));
		List<StuUser>stuList=new  ArrayList<StuUser>();
		stuList.add(new StuUser(12,"Amy"));
		stuList.add(new StuUser(23,"keke"));
		stuList.add(new StuUser(84,"Kimheechul"));
		stuList.add(new StuUser(78,"Kangkang"));
		System.out.println(max1(stuList));
		Comparator<User>c1=new Comparator<User>(){
			public int compare(User o1, User o2) {
				return o2.getAge() - o1.getAge();
			}
		};
		Comparator<StuUser>c2=new Comparator<StuUser>(){
			public int compare(StuUser o1, StuUser o2) {
				return o2.getAge() - o1.getAge();
			}
		};
		User user=new User(63);
		User user1=new User(231);
		StuUser stuUser=new StuUser(12,"Amy");
		StuUser stuUser1=new StuUser(21,"keke");
		System.out.println(myCompare(user,user1,c1));
		System.out.println(myCompare(stuUser,stuUser1,c2));
	}
}